<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('shop.market.coupon.coupon.name')"
                        type="string"
                        v-model="baTable.form.items!.name"
                        prop="name"
                        :placeholder="t('Please input field', { field: t('shop.market.coupon.coupon.name') })"
                    />
                    <FormItem
                        :label="t('shop.market.coupon.coupon.discount_type')"
                        type="radio"
                        v-model="baTable.form.items!.discount_type"
                        prop="discount_type"
                        :data="{
                            content: { '1': t('shop.market.coupon.coupon.discount_type 1'), '2': t('shop.market.coupon.coupon.discount_type 2') },
                            childrenAttr: {
                                border: true,
                            },
                        }"
                        :placeholder="t('Please select field', { field: t('shop.market.coupon.coupon.discount_type') })"
                    />
                    <InputNumber
                        v-if="baTable.form.items!.discount_type == 1"
                        :label="t('shop.market.coupon.coupon.discount_amount')"
                        prop="discount_amount"
                        :input-attr="{ step: 1, min: 1, precision: 0 }"
                        v-model.number="baTable.form.items!.discount_amount"
                        :placeholder="t('Please input field', { field: t('shop.market.coupon.coupon.discount_amount') })"
                    />
                    <InputNumber
                        v-if="baTable.form.items!.discount_type == 2"
                        :label="t('shop.market.coupon.coupon.discount_ratio')"
                        prop="discount_ratio"
                        :input-attr="{ step: 0.1, min: 1, max: 9.9, precision: 1 }"
                        v-model.number="baTable.form.items!.discount_ratio"
                        :placeholder="t('Please input field', { field: t('shop.market.coupon.coupon.discount_ratio') })"
                        tip="有效范围为1~9.9折"
                    />
                    <InputNumber
                        :label="t('shop.market.coupon.coupon.min_price')"
                        prop="min_price"
                        :input-attr="{ step: 1, min: 0.1, precision: 1 }"
                        v-model.number="baTable.form.items!.min_price"
                        :placeholder="t('Please input field', { field: t('shop.market.coupon.coupon.min_price') })"
                    />
                    <FormItem
                        :label="t('shop.market.coupon.coupon.range_type')"
                        type="radio"
                        v-model="baTable.form.items!.range_type"
                        prop="type"
                        :data="{
                            content: { '1': t('shop.market.coupon.coupon.range_type 1'), '2': t('shop.market.coupon.coupon.range_type 2') },
                            childrenAttr: {
                                border: true,
                            },
                        }"
                        :placeholder="t('Please select field', { field: t('shop.market.coupon.coupon.range_type') })"
                    />
                    <FormItem
                        v-if="baTable.form.items!.range_type == 2"
                        :label="t('shop.market.coupon.coupon.range_goods')"
                        type="remoteSelects"
                        v-model="baTable.form.items!.range_goods_ids"
                        prop="range_goods_ids"
                        :input-attr="{
                            pk: 'id',
                            field: 'name',
                            remoteUrl: '/admin/shop.goods.goods/index',
                            teleported: false,
                            popperClass: 'popper-select',
                        }"
                        :placeholder="t('Please select field', { field: t('shop.market.coupon.coupon.range_goods') })"
                    />
                    <FormItem
                        :label="t('shop.market.coupon.coupon.desc')"
                        type="textarea"
                        v-model="baTable.form.items!.desc"
                        prop="desc"
                        :input-attr="{
                            rows: 5,
                        }"
                        :placeholder="t('Please input field', { field: t('shop.market.coupon.coupon.desc') })"
                    />
                    <FormItem
                        :label="t('shop.market.coupon.coupon.status')"
                        type="radio"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :data="{
                            content: { '0': t('shop.market.coupon.coupon.status 0'), '1': t('shop.market.coupon.coupon.status 1') },
                            childrenAttr: { border: true },
                        }"
                        :placeholder="t('Please input field', { field: t('shop.market.coupon.coupon.status') })"
                        :tip="'禁用会使用户已领取的优惠券失效'"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type {FormInstance, FormItemRule} from 'element-plus'
import {inject, reactive, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import {useConfig} from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import {buildValidatorData} from '/@/utils/validate'
import InputNumber from '/@/components/shop/form/inputNumber.vue'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    name: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.name') })],
    discount_amount: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.discount_amount') })],
    discount_ratio: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.discount_ratio') })],
    min_price: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.min_price') })],
    start_time: [
        buildValidatorData({ name: 'date', title: t('shop.market.coupon.coupon.start_time') }),
        buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.start_time') }),
    ],
    end_time: [
        buildValidatorData({ name: 'date', title: t('shop.market.coupon.coupon.end_time') }),
        buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.end_time') }),
    ],
    total_num: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.total_num') })],
    remain_num: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.remain_num') })],
    limit_num: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.limit_num') })],
    discount_type: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.discount_type') })],
    range_type: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.range_type') })],
    range_goods_ids: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.range_goods') })],
    status: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.status') })],
    expiry_type: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.expiry_type') })],
    expiry_days: [buildValidatorData({ name: 'required', title: t('shop.market.coupon.coupon.expiry_days') })],
})
</script>

<style scoped lang="scss"></style>
